﻿//验证图标
var erricon = "<i class='fa fa-times-circle'></i>  ";

//生成随机数
function GetRandomNum(min, max) {
    var range = max - min;
    var rand = Math.random();
    return (min + Math.round(rand * range));
}

//时间格式化（yyyy-MM-dd）
function getNowFormatDate(str) {
    var date = new Date(str);
    var year = date.getFullYear();
    var month = date.getMonth() + 1;
    var day = date.getDate();
    var currentDate = "";
    //初始化时间 
    currentDate += year + "-";
    if (month >= 10) {
        currentDate += month + "-";
    } else {
        currentDate += "0" + month + "-";
    }
    if (day >= 10) {
        currentDate += day;
    } else {
        currentDate += "0" + day;
    }
    return currentDate;
}

//获取当前日期或当前日期前几天日期
function getCurrentDate(days) {
    var now;
    var date;
    var year;
    var month;
    var day;
    if (days > 0) {
        now = new Date();
        date = new Date(now.getTime() - days * 24 * 3600 * 1000);
        year = date.getFullYear();
        month = date.getMonth() + 1;
        day = date.getDate();
    } else {
        date = new Date();
        year = date.getFullYear();
        month = date.getMonth() + 1;
        day = date.getDate();
    }
    if (month < 10) {
        month = "0" + month;
    }
    if (day < 10) {
        day = "0" + day;
    }
    return year + '-' + month + '-' + day;
}

//计算天数差的函数，通用  
function dateDiff(sDate1, sDate2) { //sDate1和sDate2是2006-12-18格式  
    var aDate;
    var oDate1;
    var oDate2;
    var iDays;
    aDate = sDate1.split("-");
    oDate1 = new Date(aDate[1] + '-' + aDate[2] + '-' + aDate[0]); //转换为12-18-2006格式  
    aDate = sDate2.split("-");
    oDate2 = new Date(aDate[1] + '-' + aDate[2] + '-' + aDate[0]);
    iDays = parseInt(Math.abs(oDate1 - oDate2) / 1000 / 60 / 60 / 24); //把相差的毫秒数转换为天数  
    return iDays;
}


var Common = {
    /**
     * 格式化日期（不含时间）yyy-MM-dd
     */
    formatterDate: function(date) {
        if (date == undefined || date == "") {
            return "";
        }
        /*json格式时间转js时间格式*/
        date = date.substr(1, date.length - 2);
        var obj = eval('(' + "{Date: new " + date + "}" + ')');
        date = obj["Date"];
        if (date.getFullYear() < 1900) {
            return "";
        }

        var datetime = date.getFullYear()
            + "-" // "年"
            + ((date.getMonth() + 1) > 10 ? (date.getMonth() + 1) : "0"
                + (date.getMonth() + 1))
            + "-" // "月"
            + (date.getDate() < 10 ? "0" + date.getDate() : date
                .getDate());
        return datetime;
    },
    /**
     * 格式化日期（含时间"00:00:00"） yyy-MM-dd HH:mm:ss
     */
    formatterDate2: function(date) {
        if (date == undefined) {
            return "";
        }
        /*json格式时间转js时间格式*/
        date = date.substr(1, date.length - 2);
        var obj = eval('(' + "{Date: new " + date + "}" + ')');
        date = obj["Date"];
        if (date.getFullYear() < 1900) {
            return "";
        }

        /*把日期格式化*/
        var datetime = date.getFullYear()
            + "-" // "年"
            + ((date.getMonth() + 1) > 10 ? (date.getMonth() + 1) : "0"
                + (date.getMonth() + 1))
            + "-" // "月"
            + (date.getDate() < 10 ? "0" + date.getDate() : date
                .getDate()) + " " + "00:00:00";
        return datetime;
    },
    /**
     * 格式化去日期（含时间）
     */
    formatterDateTime: function(date) {
        if (date == undefined) {
            return "";
        }
        /*json格式时间转js时间格式*/
        date = date.substr(1, date.length - 2);
        var obj = eval('(' + "{Date: new " + date + "}" + ')');
        date = obj["Date"];
        if (date.getFullYear() < 1900) {
            return "";
        }

        var datetime = date.getFullYear()
            + "-" // "年"
            + ((date.getMonth() + 1) > 10 ? (date.getMonth() + 1) : "0"
                + (date.getMonth() + 1))
            + "-" // "月"
            + (date.getDate() < 10 ? "0" + date.getDate() : date
                .getDate())
            + " "
            + (date.getHours() < 10 ? "0" + date.getHours() : date
                .getHours())
            + ":"
            + (date.getMinutes() < 10 ? "0" + date.getMinutes() : date
                .getMinutes())
            + ":"
            + (date.getSeconds() < 10 ? "0" + date.getSeconds() : date
                .getSeconds());
        return datetime;
    },
    /**
     * 格式化去日期（yyyy-MM-dd HH:mm）
     */
    formatterDateTime2: function(date) {
        if (date == undefined) {
            return "";
        }
        /*json格式时间转js时间格式*/
        date = date.substr(1, date.length - 2);
        var obj = eval('(' + "{Date: new " + date + "}" + ')');
        date = obj["Date"];
        if (date.getFullYear() < 1900) {
            return "";
        }

        var datetime = date.getFullYear()
            + "-" // "年"
            + ((date.getMonth() + 1) > 10 ? (date.getMonth() + 1) : "0"
                + (date.getMonth() + 1))
            + "-" // "月"
            + (date.getDate() < 10 ? "0" + date.getDate() : date
                .getDate())
            + " "
            + (date.getHours() < 10 ? "0" + date.getHours() : date
                .getHours())
            + ":"
            + (date.getMinutes() < 10 ? "0" + date.getMinutes() : date
                .getMinutes());
        return datetime;
    }
};

function getNowFormatDate() {
    var date = new Date();
    var seperator1 = "-";
    var seperator2 = ":";
    var month = date.getMonth() + 1;
    var strDate = date.getDate();
    if (month >= 1 && month <= 9) {
        month = "0" + month;
    }
    if (strDate >= 0 && strDate <= 9) {
        strDate = "0" + strDate;
    }
    var currentdate = date.getFullYear() + seperator1 + month + seperator1 + strDate
        + " " + date.getHours() + seperator2 + date.getMinutes()
        + seperator2 + date.getSeconds();
    return currentdate;
}

// 对Date的扩展，将 Date 转化为指定格式的String
// 月(M)、日(d)、小时(h)、分(m)、秒(s)、季度(q) 可以用 1-2 个占位符， 
// 年(y)可以用 1-4 个占位符，毫秒(S)只能用 1 个占位符(是 1-3 位的数字) 
// 例子： 
// (new Date()).Format("yyyy-MM-dd hh:mm:ss.S") ==> 2006-07-02 08:09:04.423 
// (new Date()).Format("yyyy-M-d h:m:s.S")      ==> 2006-7-2 8:9:4.18 
Date.prototype.format = function(fmt) { //author: meizz 
    var o = {
        "M+": this.getMonth() + 1, //月份 
        "d+": this.getDate(), //日 
        "h+": this.getHours(), //小时 
        "m+": this.getMinutes(), //分 
        "s+": this.getSeconds(), //秒 
        "q+": Math.floor((this.getMonth() + 3) / 3), //季度 
        "S": this.getMilliseconds() //毫秒 
    };
    if (/(y+)/.test(fmt)) fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
    for (var k in o)
        if (new RegExp("(" + k + ")").test(fmt)) fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
    return fmt;
}

function dateTimeDiff(dateTime1, dateTime2) {
    var date1 = new Date(dateTime1);
    var date2 = new Date(dateTime2);

    var s1 = date1.getTime(), s2 = date2.getTime();
    var total = (s1 - s2) / 1000;

    var day = parseInt(total / (24 * 60 * 60)); //计算整数天数
    var afterDay = total - day * 24 * 60 * 60; //取得算出天数后剩余的秒数
    var hour = parseInt(afterDay / (60 * 60)); //计算整数小时数
    var afterHour = total - day * 24 * 60 * 60 - hour * 60 * 60; //取得算出小时数后剩余的秒数
    var min = parseInt(afterHour / 60); //计算整数分
    var afterMin = total - day * 24 * 60 * 60 - hour * 60 * 60 - min * 60; //取得算出分后剩余的秒数
    var value = "";
    if (day > 0) {
        value += day + '天';
    }
    if (hour > 0) {
        value += hour + '时';
    }
    if (min > 0) {
        value += min + '分';
    }
    return value;
}

//权限验证
function permissionCheck(buttonList) {
    if (!buttonList) return false;
    if (buttonList === 'null') return false;
    $.each(jQuery.parseJSON(buttonList), function(i, n) {
        var $elementBtn = $("button[buttonCode='" + n.ButtonCode + "']");
        var $elementA = $("a[buttonCode='" + n.ButtonCode + "']");
        if ($elementBtn.length > 0) {
            $elementBtn.attr('disabled', "true");
            $elementBtn.removeAttr('onclick');
        } else if ($elementA.length > 0) {
            $elementA.attr('disabled', "true");
            $elementA.removeAttr('onclick');
        }
    });
}

//iframe弹出层扩展
function iframeOpenEx(title, width, hieght, url) {
    try {
        if (url.indexOf('?') != '-1') {
            url = url + "&&random=" + GetRandomNum(1, 1000);
        } else {
            url = url + "?random=" + GetRandomNum(1, 1000);
        }
        //iframe层
        layer.open({
            type: 2,
            title: title,
            shadeClose: true,
            shade: 0.1,
            area: [width, hieght],
            content: url,
            success: function(layero, index) {
            },
            end: function() {
                var handle_status = $("#handle_status").val();
                var handle_message = $('#handle_status').attr('message');
                if (handle_status == '1') {
                    layer.msg(handle_message, {
                        icon: 1,
                        time: 500 //2秒关闭（如果不配置，默认是3秒）  
                    });
                } else if (handle_status == '2') {
                    layer.msg(handle_message, {
                        icon: 5,
                        time: 500 //2秒关闭（如果不配置，默认是3秒）  
                    });
                }
            }
        });
    } catch (err) {
        layer.msg(err.message, { icon: 5, time: 1000 });
    }
}

//关闭弹出层窗体
function iframeCloseEx() {
    try {
        parent.$('#handle_status').val(0);
        var index = parent.layer.getFrameIndex(window.name); //获取窗口索引
        parent.layer.close(index);
    } catch (err) {
        layer.msg(err.message, { icon: 5, time: 1000 });
    }
}